package resource

import (
	"demo/cfg"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

var db *sqlx.DB

// InitDB _
func InitDB() {
	dbSec := cfg.Config.Section("MYSQL")
	host, port, dbname := dbSec.Key("HOST").String(), dbSec.Key("PORT").String(), dbSec.Key("DB").String()
	user, passwd := dbSec.Key("USER").String(), dbSec.Key("PASSWD").String()
	dbDSN := fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8&loc=Local", user, passwd, host, port, dbname)
	tempDB, err := sqlx.Open("mysql", dbDSN)
	if err != nil {
		log.Fatalf("err in open mysql: %v", err)
		return
	}
	db = tempDB
}

// GetDB _
func GetDB() *sqlx.DB {
	return db
}
